package cn.zdsoft.configuration.core.standalone;

import cn.zdsoft.configuration.core.ConfigInitializer;
import cn.zdsoft.configuration.core.ExtensionLoader;
import cn.zdsoft.configuration.core.api.CallbackAble;
import cn.zdsoft.configuration.core.exceptions.ConfigurationException;
import cn.zdsoft.configuration.core.listener.ZKListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by wangdongdong on 2018/8/17 16:42.
 */
public class StandaloneConfigInitializer extends ConfigInitializer {

    private static Logger logger = LoggerFactory.getLogger(StandaloneConfigInitializer.class);

    private ZKListener listener;

    private CallbackAble callbackAble;

    @Override
    public ZKListener getListener() {
        return listener;
    }

    @Override
    public CallbackAble getCallbackAble() {
        return callbackAble;
    }

    @Override
    public void stop() {

    }

    @Override
    public boolean isStarted() {
        return false;
    }

    public static void premain(String args) {
        logger.debug("===========================================================");
        logger.debug("============read config from remote server=================");
        try {
            StandaloneConfigInitializer standaloneConfigInitializer = new StandaloneConfigInitializer();
            standaloneConfigInitializer.listener = new ZKListener();
            ExtensionLoader<CallbackAble> loader = ExtensionLoader.load(CallbackAble.class);
            standaloneConfigInitializer.callbackAble = loader.load();
            standaloneConfigInitializer.start(new StandaloneConfigContext());
        } catch (ConfigurationException e) {
            throw new Error("load configuration error");
        }
    }
}
